<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="dragon.modules.system.menu.dao.RoleMenuDaoImpl"%>
<%@page import="dragon.core.system.po.SysRoleMenu"%>
<%@page import="java.util.List"%>
<%@ page import="dragon.modules.system.org.dao.OrgDaoImpl"%>
<%@ page import="dragon.core.system.po.SysOrg"%>

<%@ page import="dragon.modules.system.user.dao.UserDaoImpl"%>
<%@ page import="dragon.core.system.po.SysUser"%>

<%@ page import="dragon.modules.system.user.dao.UserGroupDaoImpl"%>
<%@ page import="dragon.core.system.po.SysUserGroup"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<%@ include file="../../../taglib.jsp"%>
<%
	String menu = request.getParameter("ID");
	String userid = "";
	String username = "";
	String orgid = "";
	String orgname = "";
	String roleid = "";
	String rolename = "";
	RoleMenuDaoImpl rmdao = new RoleMenuDaoImpl();
	List<SysRoleMenu> rmList = rmdao.get(Integer.parseInt(menu));
	if (rmList != null) {
		if (rmList.size() > 0) {
	for (SysRoleMenu l : rmList) {
		if (l.getKind().equals("U")) {
	userid += l.getTid() + ",";
		} else if (l.getKind().equals("O")) {
	orgid += l.getTid() + ",";
		} else if (l.getKind().equals("R")) {
	roleid += l.getTid() + ",";
		}
	}

	if (userid.length() > 0) {
		userid = userid.substring(0, userid.length() - 1);
		UserDaoImpl userdao = new UserDaoImpl();
		List<SysUser> users = userdao.getUsersByIds(userid);
		if (users != null && users.size() > 0) {
	for (SysUser u : users) {
		username += u.getUsername() + ",";
	}
	username = username.substring(0, username.length() - 1);
		}
	}

	if (orgid.length() > 0) {
		orgid = orgid.substring(0, orgid.length() - 1);
		OrgDaoImpl orgdao = new OrgDaoImpl();
		List<SysOrg> orgs = orgdao.getOrgsByIds(orgid, false);
		if (orgs != null && orgs.size() > 0) {
	for (SysOrg o : orgs) {
		orgname += o.getName() + ",";
	}
	orgname = orgname.substring(0, orgname.length() - 1);
		}
	}
	if (roleid.length() > 0) {
		roleid = roleid.substring(0, roleid.length() - 1);
		UserGroupDaoImpl usergroupdao = new UserGroupDaoImpl();
		List<SysUserGroup> usergroups = usergroupdao.getUserGroupsByIds(roleid);
		if (usergroups != null && usergroups.size() > 0) {
	for (SysUserGroup u : usergroups) {
		rolename += u.getName() + ",";
	}
	rolename = rolename.substring(0, rolename.length() - 1);
		}
	}

		}
	}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Insert title here</title>
<base href="<%=basePath%>" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="css/dialog.css" />
<%@ include file="/webmodules/console/css.jsp"%>
<%@ include file="/webmodules/console/js.jsp"%>
<script>
	$(function() {
		$('#mydatagrid').datagrid({  
		    url:'dragon/modules/system/menu/action/RoleMenuHandler/authorizeList.ejw?menu=<%=menu%>',  
		    pagination:true,
		    pageSize: 15,
		    pageList: [10,15,20],
		   	loadMsg:'正在下载数据，请稍后！',
		    remoteSort:false,
		    rownumbers:true,
		    queryParams:{},
		    columns:[[{
				field : 'ck',
				checkbox : true,
				width : $(this).width() * 0.05
			},  
				{field:'MENU',title:'MENU',hidden : true},
				{field:'KIND',title:'KIND',hidden : true},
				{field:'PERMISSKIND',title:'PERMISSKIND',hidden : true},
				{field:'TID',title:'TID',hidden : true},
		        {field:'MENUNAME',title:'菜单名称',width:180},
		        {field:'KINDNAME',title:'类别',width:110,sortable:true,formatter:function(val){if(val=='U'){return '人员';}else if(val=='O'){return "部门";}else if(val=='R'){ return "角色";}}},
		        {field:'TIDNAME',title:'名称',width:100}, 
		        {field:'PERMISSKINDNAME',title:'权限来源',width:100,sortable:true} 
		    ]]  ,
		    toolbar:[{
				text:'删除',
				iconCls:'icon-remove',
				handler:function(){
					var rows = $('#mydatagrid').datagrid('getSelections');
					if (rows.length > 0) {
						top.showConfirm("删除确认","确定删除？",del,null);
					} else {
						top.showMsg("", "请选择要移除的行！", true);
					}
				}
			}
			]
		});
		$(".datagrid-toolbar").append($("#datagridsearch"));
		var p2 = $('#mydatagrid').datagrid('getPager');  
		$(p2).pagination({  
		    beforePageText: '第',
		    afterPageText: '页    共 {pages} 页',  
		    displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录'   
		}); 

		
		
		var setUser = true;
		$('#userid').combotree({
			multiple : true,
			onlyLeafCheck : true,
			onBeforeLoad : function(node, param) {
				$('#userid').combotree('setText', '<%=username%>');
			},
			onShowPanel : function() {
				var url = $('#userid').combotree('options').url;
				if (url == null || url == '') {
					$('#userid').combotree('reload', 'dragon/modules/system/user/action/UserHandler/combotree.ejw');
				}
			},
			onLoadSuccess:function(){
				if(setUser){
					$('#userid').combotree('setValues', [<%=userid%>]);
					setUser=false;
				}
			}
		});
		
		var setOrg = true;
		$('#orgid').combotree({
			onBeforeLoad : function(node, param) {
				$('#orgid').combotree('setText', '<%=orgname%>');
			},
			onShowPanel : function() {
				var url = $('#orgid').combotree('options').url;
				if (url == null || url == '') {
					$('#orgid').combotree('reload', 'dragon/modules/system/org/action/OrgHandler/combotree.ejw');
				}
			},

            	onLoadSuccess : function() {
				if (setOrg) {
					$('#orgid').combotree('setValues', [ <%=orgid%> ]);
					setOrg = false;
				}
			}
		});

		var setRole = true;
		$('#roleid').combotree({
			onBeforeLoad : function(node, param) {
				$('#roleid').combotree('setText', '<%=rolename%>');
			},
			onShowPanel : function() {
				var url = $('#roleid').combotree('options').url;
				if (url == null || url == '') {
					$('#roleid').combotree('reload', 'dragon/modules/system/user/action/UserGroupHandler/combotree.ejw');
				}
			},
			onLoadSuccess : function() {
				if (setRole) {
					$('#roleid').combotree('setValues', [<%=roleid%>]);
					setRole = false;
				}
			}
		});
	
	});
	function save() {
		var menu = $('#menu').val();
		var userid = $('#userid').combotree('getValues').join(",");
		var orgid = $('#orgid').combotree('getValues').join(",");
		var roleid = $('#roleid').combotree('getValues').join(",");
		$.post("dragon/modules/system/menu/action/RoleMenuHandler/save.ejw", {
			menu : menu,
			userid : userid,
			orgid : orgid,
			roleid : roleid
		}, function(data) {
			if (data == "t") {
			
				$('#mydatagrid').datagrid('reload');
				top.showMsg("", "保存成功！", false);
			} else {
				top.showErrorMsg("出错了", data);
			}
		});
	}
	
	function del(){
		var rows = $('#mydatagrid').datagrid('getSelections');
		var menus = new Array();
		var kinds = new Array();
		var tids = new Array();
		for ( var i = 0; i < rows.length; i++) {
			if(rows[i].PERMISSKIND=='Direct')
			{
			menus.push(rows[i].MENU);
			kinds.push(rows[i].KIND);
			tids.push(rows[i].TID);
			}
			else
			{
				  top.showMsg("", "权限为继承授权的无法删除！", true);
				  return;
			}
		}
		$.post("dragon/modules/system/menu/action/RoleMenuHandler/del.ejw", {
			menus : menus.join(","),
			kinds : kinds.join(","),
			tids : tids.join(",") 
		}, function(data) {
			if (data == "t") {
				for ( var i = 0; i < rows.length; i++) {
					var index = $('#mydatagrid').datagrid('getRowIndex', rows[i]);
					$('#mydatagrid').datagrid('deleteRow', index);
				}
				set();
				rows = null;
				top.showMsg("", "删除成功！", false);
			} else {
				top.showErrorMsg("出错了", data);
			}
		});
	}
	function set()
	{	var menu = $('#menu').val();
		$.post("dragon/modules/system/menu/action/RoleMenuHandler/getValuesAndText.ejw", {menu:menu}, function(data) {
			   var s=data.split("|");
			   var user=s[0].split(";");
			   $('#userid').combotree('setText', user[1]); 
			   $('#userid').val(user[0]);
			   var uarray=user[0].split(",");
			   $('#userid').combotree('setValues', uarray);
			   
			   var org=s[1].split(";");
				$('#orgid').combotree('setText',org[1]);
				$('#orgid').val(org[0]);
				var oarray=org[0].split(",");
				$('#orgid').combotree('setValues', oarray);
				
				 var role=s[2].split(";");
				$('#roleid').combotree('setText', role[1]);
				$('#roleid').val(role[0]);
				var rarray=role[0].split(",");
				$('#roleid').combotree('setValues',rarray);
			   
	 
	});	
	}
	function search()
	{
	  var queryParams = $('#mydatagrid').datagrid('options').queryParams;  
      queryParams.kind = $("#kind").val(); 
      $('#mydatagrid').datagrid('options').queryParams=queryParams;        
      $("#mydatagrid").datagrid('reload'); 
	}
</script>
</head>
<body>

	<div class="modartdiv" style="margin-top: 20px;float:right;width:330px;">

		<input type="hidden" value="<%=menu %>" id="menu" />
		<table width="100%" style="float:left;">
			<tr style="height: 40px;">
				<td class="table-cell-title">人员：</td>
				<td class="table-cell-input"><input id="userid" name="userid" multiple="true" cascadeCheck="false" type="text" class="easyui-combotree" style="width: 250px" value="<%=userid%>"></input></td>
			<tr>
			<tr style="height: 40px;">
				<td class="table-cell-title">部门：</td>
				<td class="table-cell-input"><input id="orgid" name="orgid" multiple="true" type="text" class="easyui-combotree" style="width: 250px" value="<%=orgid%>"></input></td>
			<tr>
			<tr style="height: 40px;">
				<td class="table-cell-title">角色：</td>
				<td class="table-cell-input"><input id="roleid" name="roleid" multiple="true" type="text" class="easyui-combotree" style="width: 250px" value="<%=roleid%>"></input></td>
			<tr>
			<tr>
				<td colspan="2" align="right" style="padding-right: 25px;padding-top: 30px;"><a href="javascript:void(0);" class="easyui-linkbutton" iconCls="icon-ok" onclick="save();">保存</a> <a href="javascript:void(0);" " class="easyui-linkbutton" iconCls="icon-cancel"
					onclick="top.closeWindow('popWindow');">关闭</a></td>
			</tr>
		</table>
	</div>
<div style="width: 550px;height:440px;float: left;">
 <div id="datagridsearch" style="margin-right:50px;">
类别<select name="kind" id="kind" onchange="search()" />
<option value="" ></option>
<option value="U">人员</option>
<option value="O">部门</option>
<option value="R">角色</option>
</select>
 </div>
<table id="mydatagrid" fit="true"></table>
</div>
</body>
</html>